<?php

namespace app\api\model;

use think\Model;

class TrainChild extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'child_id';

    public static function getChildListByTrainId($trainId)
    {
        return self::alias('a')
            ->join('video_list vl', 'vl.video_id = a.video_id')
            ->where('a.train_id', $trainId)
            ->order('a.train_sort desc,a.child_id desc')
            ->field('a.*,vl.video_title')
            ->select()
            ->toArray();
    }

    public static function getListByTrainId($trainId)
    {
        return self::alias('a')
            ->join('video_list vl', 'vl.video_id = a.video_id')
            ->join('video_category vc', 'vc.category_id = vl.category_id', 'left')
            ->where('a.train_id', $trainId)
            ->order('a.train_sort desc,a.child_id desc')
            ->field('a.*,vl.video_title,vl.video_url,vl.category_id,vc.category_func')
            ->select()
            ->toArray();
    }

    public static function countTrainTimes($trainId)
    {
        return self::where(['train_id' => $trainId])
            ->count();
    }

    public static function countFinishTimes($trainId)
    {
        return self::where(['train_id' => $trainId])
            ->where('train_status', 0)
            ->count();
    }

    public static function getInfoById($childId)
    {
        return self::where('child_id', $childId)->find();
    }
}